home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Learn C++ (CodeWarrior) / Chap 06.03 - square / square.cp < prev    next >
Text File  |  1995-10-20  |  964b  |  62 lines

  1. #include <iostream.h>
  2.  
  3.  
  4. //---------------------------------------  Rectangle
  5.  
  6. class Rectangle
  7. {
  8. //            Data members...
  9.     protected:
  10.         short    height;
  11.         short    width;
  12.  
  13. //            Member functions...
  14.     public:
  15.                 Rectangle( short heightParam, short widthParam );
  16.         void    DisplayArea();
  17. };
  18.  
  19. Rectangle::Rectangle( short heightParam, short widthParam )
  20. {
  21.     height = heightParam;
  22.     width = widthParam;
  23. }
  24.  
  25. void    Rectangle::DisplayArea()
  26. {
  27.     cout << "Area is: " <<
  28.         height * width << '\n';
  29. }
  30.  
  31.  
  32. //---------------------------------------  Rectangle:Square
  33.  
  34. class Square : public Rectangle
  35. {
  36. //            Data members...
  37.  
  38. //            Member functions...
  39.     public:
  40.                 Square( short side );
  41. };
  42.  
  43. Square::Square( short side ) : Rectangle( side, side )
  44. {
  45. }
  46.  
  47.  
  48. //---------------------------------------  main()
  49.  
  50. int    main()
  51. {
  52.     Square        *mySquare;
  53.     Rectangle    *myRectangle;
  54.     
  55.     mySquare = new Square( 10 );
  56.     mySquare->DisplayArea();
  57.     
  58.     myRectangle = new Rectangle( 10, 15 );
  59.     myRectangle->DisplayArea();
  60.     
  61.     return 0;
  62. }